﻿/*
This file is part of Claymore.
Copyright (C) 2009, Fabrice Michellonet (fabrice dot michellonet at gmail dot com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Configuration;
using ClaymoreConf = Claymore.Configuration;
using System.Reflection;

namespace Claymore
{
    public class Claymore
    {

        #region Fields

        private static IViewManager _viewMngr;
        private static IPresenterManager _presenterMngr;

        #endregion

        /// <summary>
        /// Hide the default constructor.
        /// </summary>
        private Claymore(){}

        /// <summary>
        /// Gets the view manager.
        /// </summary>
        /// <value>The view manager.</value>
        public static IViewManager ViewManager
        {
            get { return _viewMngr; }
        }

        /// <summary>
        /// Gets the presenter manager.
        /// </summary>
        /// <value>The presenter manager.</value>
        public static IPresenterManager PresenterManager
        {
            get { return _presenterMngr; }
        }

        /// <summary>
        /// Initializes Claymore engine.
        /// </summary>
        public static void Initialize() {
            _viewMngr = new ClaymoreViewManager();
            _presenterMngr = new ClaymorePresenterManager();

            initialize(Assembly.GetCallingAssembly());
        }

        /// <summary>
        /// Initializes Claymore engine and load the specified config.
        /// </summary>
        /// <param name="configSectionName">Name of the config section.</param>
        public static void Initialize(string configSectionName)
        {
            LoadConfig(configSectionName);
            initialize(Assembly.GetCallingAssembly());
        }

        /// <summary>
        /// Initializes Claymore engine using the specified assembly.
        /// </summary>
        /// <param name="asm">The asm.</param>
        private static void initialize(Assembly asm)
        {
            _viewMngr.AddViewsAssembly(asm);
            _presenterMngr.AddPresentersAssembly(asm);
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        public static void LoadConfig(string sectionName) {
            ClaymoreConf.ConfigSectionHandler config =
                (ClaymoreConf.ConfigSectionHandler)ConfigurationManager.GetSection(sectionName);


            if (!string.IsNullOrEmpty(config.ViewManager.TypeName))
            {
                Type viewMngrType = Type.GetType(config.ViewManager.TypeName);
                
                if (viewMngrType.IsAssignableFrom(typeof (IViewManager)))
                    _viewMngr = (IViewManager) Activator.CreateInstance(viewMngrType);
            }
            else
                _viewMngr = new ClaymoreViewManager();


            if (!string.IsNullOrEmpty(config.PresenterManager.TypeName))
            {
                Type presMngrType = Type.GetType(config.PresenterManager.TypeName);

                if (presMngrType.IsAssignableFrom(typeof(IPresenterManager)))
                    _presenterMngr = (IPresenterManager)Activator.CreateInstance(presMngrType);
            }
            else
                _presenterMngr = new ClaymorePresenterManager();

            // Add additional referenced views.
            foreach (ClaymoreConf.Assembly viewAssembly in config.ViewsAssemblies)
            {
                Assembly asm = Assembly.Load(viewAssembly.AssemblyName);
                ViewManager.AddViewsAssembly(asm);
            }

            // Add additional referenced presenters.
            foreach (ClaymoreConf.Assembly presenterAssembly in config.PresentersAssemblies)
            {
                Assembly asm = Assembly.Load(presenterAssembly.AssemblyName);
                _presenterMngr.AddPresentersAssembly(asm);
            }
        }
    }
}